package com.itheima.jobs;

import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SetReservationCount {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private JedisPool jedis;

    public void run() {
        //使用calendar获取下月数据
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.MONTH,1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(calendar.getTime());

        //获取当前时间
        calendar.add(calendar.MONTH,-1);
        String now = sdf.format(calendar.getTime());

        //查询出所有符合的sql
        String sql = "SELECT * FROM t_ordersetting WHERE orderDate LIKE ?";

        List<OrderSetting> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(OrderSetting.class), format+"%");

        int count = 300;

        String reservation = jedis.getResource().get("setReservation");

        if(reservation!=null && reservation.split(",")[0].substring(1).equals(now)){
            String reservationCount = reservation.split(",")[1];
            count = Integer.parseInt(reservationCount.substring(0,reservationCount.length()-1));
        }

        //获取所有周末
        List<String> weekendInMonth = getWeekendInMonth(format);


        //修改所有的值
        if(list != null) {
            for (OrderSetting orderSetting : list) {
                    jdbcTemplate.update("UPDATE t_ordersetting SET  number = ? WHERE id =?",count,orderSetting.getId());

            }
        }

        jedis.close();

        //遍历list,获取出所有的id
        if (list != null) {
            try {
                for (String str : weekendInMonth) {
                    for (OrderSetting orderSetting : list) {
                        String time = DateUtils.parseDate2String(orderSetting.getOrderDate(),"yyyy-MM-dd");

                        if(str.equals(time)){
                            jdbcTemplate.update("UPDATE t_ordersetting SET  number = 0 WHERE id =?",orderSetting.getId());
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 获取当月的所有周末
     * @param date
     * @return
     */
    public static List<String> getWeekendInMonth(String date) {
        Integer year = Integer.parseInt(date.split("-")[0]);
        Integer month = Integer.parseInt(date.split("-")[1]);
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);// 不设置的话默认为当年
        calendar.set(Calendar.MONTH, month - 1);// 设置月份
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为当月第一天
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);// 当月最大天数
        for (int i = 0; i < daySize-1; i++) {
            calendar.add(Calendar.DATE, 1);//在第一天的基础上加1
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (week == Calendar.SATURDAY || week == Calendar.SUNDAY) {// 1代表周日，7代表周六 判断这是一个星期的第几天从而判断是否是周末
                Integer day = calendar.get(Calendar.DAY_OF_MONTH);
                String time = ""+day;
                if(day < 10) {
                    time = "0"+day;
                }
                list.add(year+"-"+month+"-"+time);// 得到当天是一个月的第几天
            }
        }
        return list;
    }
}
